package com.example.demo.controller;

import com.example.demo.bean.Code;
import com.example.demo.dao.CodeMapper;
import com.example.demo.utils.RedisUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

@Controller
@CrossOrigin
@RequestMapping("/code")
public class CodeController {
    @Autowired
    private CodeMapper codeMapper;
    @Autowired
    private RedisUtils redisUtils;
    /*
    * 发送图片验证码
    * */
    @RequestMapping("/getcode")
    @ResponseBody                                   //把验证码的url图片地址给前端，并把code保存到session!
    public String getCode(HttpSession session){
        Code code=codeMapper.queryCode();
        //session.setAttribute("code",code.getCode());         //存入session,单机
        redisUtils.set("code",code.getCode());                   //存redis，集群
        System.out.println("验证码发送成功!");
        return code.getUrl();
    }
}
